package org.paradigmshift.timelapse.rest

import java.net.URI

import scala.actors.Actor

import org.apache.http.client.utils.URIBuilder
import org.paradigmshift.timelapse.actor.messages.Exit
import org.paradigmshift.timelapse.exceptions.TimeLapseException

case class SetUrl( scheme: String, host: String, port: Int )
case class GetUrl
  
class ServerUriActor extends Actor {
  
  private var serverUrl: Option[URI] = None
  
  start()
  
  private def buildConnectionUrl( scheme: String, host: String, port: Int ) {
    val uriBuilder = new URIBuilder()
    uriBuilder.setScheme( scheme )
    uriBuilder.setHost( host )
    uriBuilder.setPort( port )
    serverUrl = Option( uriBuilder.build() )
  }
  
  def act() {
    loop {
      react {
        case SetUrl( scheme, host, port ) => buildConnectionUrl( scheme, host, port )
        case GetUrl => reply( serverUrl )
        case Exit => exit
        case _ => throw new TimeLapseException( "Invalid message" )
      }
    }
  }
}